home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Communications / pcomm / Source / tty_ucb.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-06-12  |  5.1 KB  |  305 lines

  1. /*
  2.  * Berkeley specific routines for manipulating the TTY
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include <sgtty.h>
  7. #include <fcntl.h>
  8. #include "dial_dir.h"
  9. #include "modem.h"
  10. #include "param.h"
  11.  
  12. static struct sgttyb hold;
  13.  
  14. /*
  15.  * Change the communication line settings to the new values.
  16.  */
  17.  
  18. void
  19. line_set()
  20. {
  21.     static int first = 1;
  22.     extern int fd;
  23.     struct sgttyb tbuf;
  24.     int baud;
  25.  
  26.     if (first) {
  27.         ioctl(fd, TIOCGETP, &hold);
  28.         first = 0;
  29.     }
  30.  
  31.     /*
  32.      * The manual dial entry also serves to store the previous
  33.      * line settings.  How else would the manual dial entry
  34.      * know what line setting to use?
  35.      */
  36.     if (dir->d_cur != 0) {
  37.         dir->baud[0] = dir->baud[dir->d_cur];
  38.         dir->parity[0] = dir->parity[dir->d_cur];
  39.         dir->dbits[0] = dir->dbits[dir->d_cur];
  40.         dir->sbits[0] = dir->sbits[dir->d_cur];
  41.     }
  42.                     /* nothing to do! */
  43.     if (fd == -1)
  44.         return;
  45.                     /* get the current settings */
  46.     ioctl(fd, TIOCGETP, &tbuf);
  47.                     /* set some beginning values */
  48.     tbuf.sg_flags = CBREAK;
  49.  
  50.     if (*param->flow == 'X')
  51.         tbuf.sg_flags |= TANDEM;
  52.                     /* the baud rate */
  53.     baud = modem->init_sp[modem->m_cur];
  54.     if (baud == 0)
  55.         baud = dir->baud[dir->d_cur];
  56.  
  57.     switch (baud) {
  58.         case 300:
  59.             tbuf.sg_ispeed = B300;
  60.             tbuf.sg_ospeed = B300;
  61.             break;
  62.         case 1200:
  63.             tbuf.sg_ispeed = B1200;
  64.             tbuf.sg_ospeed = B1200;
  65.             break;
  66.         case 2400:
  67.             tbuf.sg_ispeed = B2400;
  68.             tbuf.sg_ospeed = B2400;
  69.             break;
  70.         case 4800:
  71.             tbuf.sg_ispeed = B4800;
  72.             tbuf.sg_ospeed = B4800;
  73.             break;
  74.         case 9600:
  75.             tbuf.sg_ispeed = B9600;
  76.             tbuf.sg_ospeed = B9600;
  77.             break;
  78.         case 19200:
  79. #ifdef B19200
  80.             tbuf.sg_ispeed = B19200;
  81.             tbuf.sg_ospeed = B19200;
  82. #else /* B19200 */
  83. #ifdef EXTA
  84.             tbuf.sg_ispeed = EXTA;
  85.             tbuf.sg_ospeed = EXTA;
  86. #endif /* EXTA */
  87. #endif /* B19200 */
  88.             break;
  89.     }
  90.                     /* the parity */
  91.     switch (dir->parity[dir->d_cur]) {
  92.         case 'N':
  93.             tbuf.sg_flags |= RAW;
  94.             break;
  95.         case 'O':
  96.             tbuf.sg_flags |= ODDP;
  97.             break;
  98.         case 'E':
  99.             tbuf.sg_flags |= EVENP;
  100.             break;
  101.     }
  102.                     /* now set 'em! */
  103.     ioctl(fd, TIOCSETP, &tbuf);
  104.     ioctl(fd, TIOCHPCL, 0);
  105.     return;
  106. }
  107.  
  108. /*
  109.  * Put things back the way they were.
  110.  */
  111.  
  112. void
  113. reset_line()
  114. {
  115.     extern int fd;
  116.  
  117.     ioctl(fd, TIOCSETP, &hold);
  118.     return;
  119. }
  120.  
  121. /*
  122.  * Put the stdin/stdout in terminal mode.  We've divided up the
  123.  * responsibility for the line settings options between the serial port
  124.  * and the stdin and stdout.
  125.  */
  126.  
  127. void
  128. term_mode()
  129. {
  130.     struct sgttyb tbuf;
  131.  
  132.     ioctl(0, TIOCGETP, &tbuf);
  133.     
  134.     tbuf.sg_flags |= RAW;
  135.     tbuf.sg_flags &= ~(CRMOD|ECHO);
  136.  
  137.     if (dir->duplex[dir->d_cur] == 'H')
  138.         tbuf.sg_flags |= ECHO;
  139.  
  140.     ioctl(0, TIOCSETP, &tbuf);
  141.     return;
  142. }
  143.  
  144. /*
  145.  * Put the TTY driver in the mode suitable for xmodem transfers.
  146.  */
  147.  
  148. void
  149. xmodem_mode(fds)
  150. int fds;
  151. {
  152.     struct sgttyb tbuf;
  153.  
  154.     ioctl(fds, TIOCGETP, &tbuf);
  155.     /*
  156.      * Turn off the XON/XOFF flow control, turn off echoing, and
  157.      * switch to 8 bit no parity.
  158.      */
  159.     tbuf.sg_flags |= (RAW|ANYP);
  160.     tbuf.sg_flags &= ~ECHO;
  161.     ioctl(fds, TIOCSETP, &tbuf);
  162.     return;
  163. }
  164.  
  165. /*
  166.  * Put the TTY line in a mode suitable for the ASCII transfer.
  167.  */
  168.  
  169. void
  170. ascii_mode(up)
  171. int up;
  172. {
  173.     extern int fd;
  174.     struct sgttyb tbuf;
  175.  
  176.     ioctl(fd, TIOCGETP, &tbuf);
  177.  
  178.     tbuf.sg_flags |= (CBREAK|TANDEM);
  179.     tbuf.sg_flags &= ~(RAW|CRMOD|ECHO|CRDELAY);
  180.  
  181.     if (up) {
  182.                     /* CR delay times */
  183.         switch (param->cr_delay) {
  184.             case 0:
  185.                 break;
  186.             case 100:
  187.                 tbuf.sg_flags |= CR1;
  188.                 break;
  189.             case 150:
  190.                 tbuf.sg_flags |= CR2;
  191.                 break;
  192.         }
  193.     }
  194.  
  195.     ioctl(fd, TIOCSETP, &tbuf);
  196.     return;
  197. }
  198.  
  199. /*
  200.  * Flush the file descriptor.  Very messy... flushing the input causes a
  201.  * wait for the output to drain, and there is no output flushing.
  202.  */
  203.  
  204. int
  205. tty_flush(fds, mode)
  206. int fds, mode;
  207. {
  208.     int ret_code = 0;
  209.     struct sgttyb tbuf;
  210.  
  211.     switch(mode) {
  212.         case 0:            /* flush input queue */
  213.             ioctl(fds, TIOCGETP, &tbuf);
  214.             ioctl(fds, TIOCSETP, &tbuf);
  215.             break;
  216.         case 1:            /* flush output queue */
  217.             /* sorry! */
  218.             break;
  219.         case 2:            /* flush both input and output */
  220.             ioctl(fds, TIOCFLUSH, 0);
  221.             break;
  222.         default:
  223.             ret_code++;
  224.             break;
  225.     }
  226.     return(ret_code);
  227. }
  228.  
  229. /*
  230.  * Wait for the output to drain
  231.  */
  232.  
  233. int
  234. tty_drain(fds)
  235. int fds;
  236. {
  237.     struct sgttyb tbuf;
  238.                     /* this flushes the input too */
  239.     ioctl(fds, TIOCGETP, &tbuf);
  240.     return(ioctl(fds, TIOCSETP, &tbuf));
  241. }
  242.  
  243. /*
  244.  * Send a modem break
  245.  */
  246.  
  247. int
  248. tty_break(fds)
  249. int fds;
  250. {
  251.     unsigned int sleep();
  252.  
  253.     ioctl(fds, TIOCSBRK, (struct sgttyb *) 0);
  254.     sleep(1);
  255.     return(ioctl(fds, TIOCCBRK, (struct sgttyb *) 0));
  256. }
  257.  
  258. /*
  259.  * Fix the file descriptor so that a read is satisfied immediately.  When
  260.  * read() is called it returns the character in the queue, or an error if
  261.  * no key was pressed.
  262.  */
  263.  
  264. int
  265. tty_noblock(fds, on)
  266. int fds, on;
  267. {
  268.     int current;
  269.  
  270.     current = fcntl(fds, F_GETFL, 0);
  271.     if (on)
  272.         return(fcntl(fds, F_SETFL, current | FNDELAY));
  273.     else
  274.         return(fcntl(fds, F_SETFL, current & ~FNDELAY));
  275. }
  276.  
  277. /*
  278.  * Get the current baud rate of the terminal
  279.  */
  280.  
  281. int
  282. my_speed()
  283. {
  284.     static int speed[15] = {0, 50, 75, 110, 134, 150, 200, 300, 600,
  285.      1200, 1800, 2400, 4800, 9600, 19200};
  286.     struct sgttyb tbuf;
  287.  
  288.     ioctl(0, TIOCGETP, &tbuf);
  289.     return(speed[tbuf.sg_ispeed]);
  290. }
  291.  
  292. /*
  293.  * Restart any XON/XOFF flow control that may have stopped the tty 
  294.  */
  295.  
  296. void
  297. tty_restart()
  298. {
  299.     extern int fd;
  300.  
  301.     if (fd != -1 && *param->flow == 'X')
  302.         ioctl(fd, TIOCFLUSH, (struct sgttyb *) 0);
  303.     return;
  304. }
  305.